{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import requests "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "AK='3xHnQw69iFkGtvfaHSaQp28F'\n",
    "\n",
    "SK='5IrxRKBGsOSUKe9pOGTEHPlRDGWSjso5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.61dffbcb6a4c227b9fd7edb4e29a85ad.2592000.1660105146.282335-25869166'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=3xHnQw69iFkGtvfaHSaQp28F&client_secret=5IrxRKBGsOSUKe9pOGTEHPlRDGWSjso5'\n",
    "response = requests.get(host)\n",
    "my_token=response.json()['access_token']\n",
    "my_token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.e8b24e3603655ab20995f0fc52c3db5f.2592000.1660067863.282335-25869166'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_token=response.json()['access_token']\n",
    "my_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——通用物体识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '办公桌', 'score': 0.811442, 'root': '商品-家具'}, {'keyword': '电脑', 'score': 0.64264, 'root': '商品-电脑办公'}, {'keyword': '笔记本电脑', 'score': 0.413603, 'root': '商品-电脑办公'}, {'keyword': '台式电脑', 'score': 0.168309, 'root': '商品-数码产品'}, {'keyword': '室内一角', 'score': 6.8e-05, 'root': '建筑-室内'}], 'log_id': 1546348490853102420}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/wt1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '化妆品', 'score': 0.30702, 'root': '美妆-香水彩妆'}, {'keyword': '美妆', 'score': 0.26732, 'root': '商品-美妆'}, {'keyword': '护肤品', 'score': 0.21144, 'root': '商品-护肤品'}, {'keyword': '化妆/护肤品', 'score': 0.106805, 'root': '商品-美妆'}, {'keyword': '瓶子', 'score': 0.010457, 'root': '商品-容器'}], 'log_id': 1546348502363844735}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/wt3.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——植物识别API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.51942223, 'name': '粉团'}, {'score': 0.28503406, 'name': '绣球荚蒾'}, {'score': 0.06679584, 'name': '绣球'}], 'log_id': 1546192044673730301}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/zw_1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token =my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.70369667, 'name': '蒲公英'}, {'score': 0.08794005, 'name': '辽东蒲公英'}, {'score': 0.032075584, 'name': '药用蒲公英'}], 'log_id': 1546192048006888133}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/zw_2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token =my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——动物识别API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': '0.84738', 'name': '棕熊'}, {'score': '0.0962287', 'name': '狗熊'}, {'score': '0.0300487', 'name': '大灰熊'}, {'score': '0.0152178', 'name': '北美灰熊'}, {'score': '0.00144981', 'name': '美洲黑熊'}, {'score': '0.000917135', 'name': '棕色大熊猫'}], 'log_id': 1546192054329511680}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "动物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/dw_2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——果蔬识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'score': 0.740366, 'name': '非果蔬食材'}, {'score': 0.1524327, 'name': '西柚'}, {'score': 0.03563046, 'name': '葡萄柚'}, {'score': 0.0076256297, 'name': '橙子'}, {'score': 0.0063300137, 'name': '泰国柠檬'}], 'log_id': 1546192061406271654}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "细粒度图像识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/gs1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'score': 0.5679751, 'name': '猕猴桃'}, {'score': 0.39715987, 'name': '奇异果'}, {'score': 0.015998494, 'name': '黄水猕猴桃'}, {'score': 0.0055706007, 'name': '红阳猕猴桃'}, {'score': 0.0045568794, 'name': '金艳猕猴桃'}], 'log_id': 1546192067456255414}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "细粒度图像识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/gs2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——菜品识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '宫保鸡丁', 'calorie': '197', 'probability': '0.327426', 'has_calorie': True}, {'name': '红烧肉', 'calorie': '227', 'probability': '0.161467', 'has_calorie': True}, {'name': '烧排骨', 'calorie': '135', 'probability': '0.0669959', 'has_calorie': True}, {'name': '拼盘', 'probability': '0.0369422', 'has_calorie': False}, {'name': '虾球', 'calorie': '91', 'probability': '0.0324323', 'has_calorie': True}], 'log_id': 1546192074647810973}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/cp2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":5}\n",
    "access_token =my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '沙拉', 'calorie': '724', 'probability': '0.456864', 'has_calorie': True}, {'name': '蔬菜沙拉', 'calorie': '39', 'probability': '0.147013', 'has_calorie': True}, {'name': '凯撒沙拉', 'calorie': '72', 'probability': '0.125278', 'has_calorie': True}, {'name': '非菜', 'probability': '0.058387', 'has_calorie': False}, {'name': '肉沙拉', 'calorie': '229', 'probability': '0.0299343', 'has_calorie': True}], 'log_id': 1546192081201444996}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/cp1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":5}\n",
    "access_token =my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——地标识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'landmark': '外滩'}, 'log_id': 1546192085312476697}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "地标识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/db1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'landmark': '天坛'}, 'log_id': 1546192092022442677}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "地标识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/db2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——红酒识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'countryEn': 'Chile', 'grapeEn': '', 'grapeCn': '', 'color': '深紫红色/Dark Violet', 'regionCn': '中央谷地', 'regionEn': 'Central Valley', 'subRegionEn': 'Colchagua Valley', 'description': '此酒呈深紫红色，散发黑果风味及带点凉爽地区马齐古的凯奈尔的辛辣轻快果香，亦有小味儿多的强烈黑浆果味的香气，酒体丰厚，大量成熟的单宁赋予收敛感并令酒体结构更佳。', 'subRegionCn': '空加瓜谷', 'classifyByColor': '红葡萄酒/Red Wine', 'wineryEn': 'Montes Wines', 'wineryCn': '蒙特斯酒庄', 'classifyBySugar': '干型/Dry', 'wineNameEn': 'Montes Purple Angel', 'wineNameCn': '蒙特斯紫天使红葡萄酒', 'tasteTemperature': '14-18℃', 'hasdetail': 1, 'countryCn': '智利'}, 'log_id': 1546192106854933673}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "细粒度图像识别—红酒识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/hj1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token =my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用图像识别——车辆识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'location_result': {'top': 208, 'left': 50, 'width': 388, 'height': 242}, 'color_result': '黑色', 'result': [{'year': '2021', 'name': '大众桑塔纳(Santana)', 'score': 0.9066367745399475}, {'year': '2020', 'name': '大众宝来', 'score': 0.0008616680861450732}, {'year': '2021', 'name': '大众捷达', 'score': 0.0003270757151767612}, {'year': '2021', 'name': '大众速腾', 'score': 0.00018923549214378}, {'year': '2018', 'name': '大众甲壳虫', 'score': 0.0001512948947492987}], 'log_id': 1546192117664594435}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "车型识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/car\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三下-API/week19期末项目/images/cl1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":5}\n",
    "access_token = my_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用星座运势API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建字典并测试API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#构建星座对应编码字典\n",
    "dict_xz={'白羊座':'%E7%99%BD%E7%BE%8A%E5%BA%A7',\n",
    " '金牛座':'%E9%87%91%E7%89%9B%E5%BA%A7',\n",
    " '双子座':'%E5%8F%8C%E5%AD%90%E5%BA%A7',\n",
    " '巨蟹座':'%E5%B7%A8%E8%9F%B9%E5%BA%A7',\n",
    " '狮子座':'%E7%8B%AE%E5%AD%90%E5%BA%A7',\n",
    " '处女座':'%E5%A4%84%E5%A5%B3%E5%BA%A7',\n",
    " '天秤座':'%E5%A4%A9%E7%A7%A4%E5%BA%A7',\n",
    " '天蝎座':'%E5%A4%A9%E8%9D%8E%E5%BA%A7',\n",
    " '射手座':'%E5%B0%84%E6%89%8B%E5%BA%A7',\n",
    " '魔羯座':'%E6%91%A9%E7%BE%AF%E5%BA%A7',\n",
    " '摩羯座':'%E6%91%A9%E7%BE%AF%E5%BA%A7',\n",
    " '水瓶座':'%E6%B0%B4%E7%93%B6%E5%BA%A7',\n",
    " '双鱼座':'%E5%8F%8C%E9%B1%BC%E5%BA%A7'}\n",
    "\n",
    "#构建日期对应字典\n",
    "dict_sj={\n",
    "    '今天':'today',\n",
    "    '今日':'today',\n",
    "    '本日':'today',\n",
    "    '明天':'tomorrow',\n",
    "    '明日':'tomorrow',\n",
    "    '本周':'week',\n",
    "    '本月':'month',\n",
    "    '本年':'year',\n",
    "    '今年':'year',\n",
    "    '这一年':'year'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试取出对应浏览器编码\n",
    "string='魔羯座'\n",
    "s1=dict_xz[string]\n",
    "#测试取出对应日期key\n",
    "str_time='本年'\n",
    "s2=dict_sj[str_time]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '摩羯座',\n",
       " 'date': '2022年',\n",
       " 'year': 2022,\n",
       " 'mima': {'info': '变得更加豁达开放',\n",
       "  'text': ['一向严肃的摩羯座在2022年能够变得更加豁达开放，你们能以比较乐观的心态投入新的一年的生活中，摆脱过去的压抑和严肃，重新探索一种新的行为方式。这样的改变能够使你过的轻松许多，达成的成就却丝毫没有减少。摩羯座2022年可以佩戴一个克洛诺斯土星双环扣吉宏项链作为全年的幸运护身符饰物；项链由两个纯银的平安扣相互紧扣而成，项链上有“Cronus”字母，代表摩羯座的守护神克洛诺斯，环扣上由摩羯座符号和守护星“土星”图案组成，一环扣一环的双环平安扣紧密相依，象征着坚韧不催的毅力和勇气，激励和守护着摩羯们在2022年里勇往直前、顺风顺水。']},\n",
       " 'career': ['一分耕耘一分收获是摩羯们今年的主题。已经工作了的摩羯们能够越挫越勇，在去年的基础上再创新高，处于逆境的小伙伴们也能够借此机会逆风翻盘，积累自己的实力。还在读书的小伙伴们只要沉下心来积淀自己，就一定可以取得可观的好成绩。'],\n",
       " 'love': ['单身的摩羯座们在今年仍然保持着比较高的姿态，过于高冷的态度可能会使你错过质量不错的桃花。已经有伴的小伙伴们则容易与伴侣产生一些新人方面的问题，也较为容易盲目猜忌对方。'],\n",
       " 'health': ['部分摩羯可能会因运动过度出现扭伤、拉伤或肌肉酸痛的情况。试着保持合适的运动量，并且在运动过后好好放松自己吧。'],\n",
       " 'finance': ['在新的一年中，摩羯们的财运四平八稳，正财方面工资会水涨船高一些，但在偏财方面则不太理想，手头的投资项目可能取得不了想要的利益。摩羯座今年可佩戴一串黄水晶笑口财神宝懿手链来提升金钱指数，此手链由摩羯座今年的财富主石黄水晶构成；其中男款宝石为银曜石与黄水晶组合而成，女款宝石则为紫水晶与黄水晶组成；而笑口常开的小财神喜庆吉祥，带来福气满满，寓意摩羯们2022年福运临门，称心如意。'],\n",
       " 'luckeyStone': '紫黄晶',\n",
       " 'future': '',\n",
       " 'resultcode': '200',\n",
       " 'error_code': 0}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "url_xz='http://web.juhe.cn/constellation/getAll'\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "my_key=\"18e9cd33d5c7ae27cf70a96147d5f4c6\"\n",
    "request_url=url_xz+'?consName='+s1+'&type='+s2+'&key='+my_key\n",
    "request_url\n",
    "response = requests.get(request_url, headers=headers)\n",
    "res=response.json()\n",
    "res\n",
    "# if response:\n",
    "#     print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 包装成函数方便调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xingzuo_cx(string,str_time):\n",
    "    import requests \n",
    "    #构建星座对应编码字典\n",
    "    dict_xz={'白羊座':'%E7%99%BD%E7%BE%8A%E5%BA%A7',\n",
    "     '金牛座':'%E9%87%91%E7%89%9B%E5%BA%A7',\n",
    "     '双子座':'%E5%8F%8C%E5%AD%90%E5%BA%A7',\n",
    "     '巨蟹座':'%E5%B7%A8%E8%9F%B9%E5%BA%A7',\n",
    "     '狮子座':'%E7%8B%AE%E5%AD%90%E5%BA%A7',\n",
    "     '处女座':'%E5%A4%84%E5%A5%B3%E5%BA%A7',\n",
    "     '天秤座':'%E5%A4%A9%E7%A7%A4%E5%BA%A7',\n",
    "     '天蝎座':'%E5%A4%A9%E8%9D%8E%E5%BA%A7',\n",
    "     '射手座':'%E5%B0%84%E6%89%8B%E5%BA%A7',\n",
    "     '魔羯座':'%E6%91%A9%E7%BE%AF%E5%BA%A7',#考虑到摩羯座书写方式不一样\n",
    "     '摩羯座':'%E6%91%A9%E7%BE%AF%E5%BA%A7',\n",
    "     '水瓶座':'%E6%B0%B4%E7%93%B6%E5%BA%A7',\n",
    "     '双鱼座':'%E5%8F%8C%E9%B1%BC%E5%BA%A7'}\n",
    "\n",
    "    #构建日期对应字典\n",
    "    dict_sj={\n",
    "        '今天':'today',\n",
    "        '今日':'today',\n",
    "        '本日':'today',\n",
    "        '明天':'tomorrow',\n",
    "        '明日':'tomorrow',\n",
    "        '本周':'week',\n",
    "        '本月':'month',\n",
    "        '本年':'year',\n",
    "        '今年':'year',\n",
    "        '这一年':'year'\n",
    "    }\n",
    "    #测试取出对应浏览器编码\n",
    "#     string='魔羯座'\n",
    "    s1=dict_xz[string]\n",
    "    #测试取出对应日期key\n",
    "#     str_time='本年'\n",
    "    s2=dict_sj[str_time] \n",
    "\n",
    "    url_xz='http://web.juhe.cn/constellation/getAll'\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    my_key=\"18e9cd33d5c7ae27cf70a96147d5f4c6\"\n",
    "    request_url=url_xz+'?consName='+s1+'&type='+s2+'&key='+my_key\n",
    "    request_url\n",
    "    response = requests.get(request_url, headers=headers)\n",
    "    res=response.json()\n",
    "    \n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '双鱼座',\n",
       " 'date': '2022年',\n",
       " 'year': 2022,\n",
       " 'mima': {'info': '需要多多倾诉，不妨去影院看几场电影吧。',\n",
       "  'text': ['双鱼座们今年的运势大体而言不错。由于水星和木星的影响，你们在新的一年里工作学习压力会略微有些增大，大家可以找个值得信任的人倾诉一番，你们需要靠倾诉来缓解自己内心的压力。当然，见人就说个不停也是不合适的，和对的人倾诉才能够真正缓解压力，改变自己的心情。双鱼座2022年可佩戴一个波塞冬海王星双环扣吉宏项链作为全年的幸运护身符饰物；项链由两个纯银的平安扣相互紧扣而成，项链上有“Poseidon”字母，代表双鱼座的守护神海神波塞冬，环扣上由双鱼座符号和守护星“海王星”图案组成，一环扣一环的双环平安扣紧密相依，象征着坚韧不催的毅力和勇气，激励和守护着双鱼们在2022年里勇往直前、顺风顺水。']},\n",
       " 'career': ['双鱼们今年的工作态度一直都是兢兢业业的，他们会把自己手头上面的任务认认真真的完成，不会有一丝一毫的懈怠。如此认真的态度自然是能够把事业学业搞得风生水起，不过凡事都需要一个过程，还需要再耐心去坚持一段时间才能见到成效。'],\n",
       " 'love': ['今年双鱼们的爱情运势有些低迷，单身的小伙伴们暂时不会有想要找对象的想法，所以就算缘分摆在面前，他们也难以主动把握住。已经有伴的双鱼们则需要增加自己与对方的共同话题，以免产生无聊、苦闷的心情。'],\n",
       " 'health': ['部分双鱼座会出现牙龈肿痛、口腔溃疡等口腔问题，大家一定要多吃一些泻火清热的蔬菜水果，以免上火的问题。'],\n",
       " 'finance': ['随着双鱼们收入的回升，很多小伙伴就会开始产生存钱的想法了，他们会把手头上多余的金钱的一部分先存下来当做积蓄，以防之后的不时之需。如此一来不但稳妥许多，心里也会比较有安全感。双鱼座今年可佩戴一串多宝石鸢尾花宝懿手链来提升金钱指数，此手链由多种财富宝石构成；其中男款宝石为黄虎眼、蓝砂石、月光石、黑曜石、地图石等组合，女款宝石则为七彩玉髓组成；而鸢尾花乃古希腊象征财富与尊贵的幸运物，寓意双鱼们在2022年财气满满，步步高升。'],\n",
       " 'luckeyStone': '青金石',\n",
       " 'future': '',\n",
       " 'resultcode': '200',\n",
       " 'error_code': 0}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一个位置参数填写星座名称,第二个位置参数填写时间范围\n",
    "# 时间范围可选:'今天/今日/本日'、'明天/明日'、'本月'、'本年/今年/这一年'\n",
    "xingzuo_cx('双鱼座','本年')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用历史上的今天API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试接口调用代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'reason': 'success',\n",
       " 'result': [{'day': '7/9',\n",
       "   'date': '781年7月9日',\n",
       "   'title': '唐朝杰出将领郭子仪逝世',\n",
       "   'e_id': '8547'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1085年7月9日',\n",
       "   'title': '以天理为本的理学宗师程颢逝世',\n",
       "   'e_id': '8548'},\n",
       "  {'day': '7/9', 'date': '1357年7月9日', 'title': '查理大桥奠基', 'e_id': '8549'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1582年7月9日',\n",
       "   'title': '明朝“宰相之杰”张居正逝世',\n",
       "   'e_id': '8550'},\n",
       "  {'day': '7/9', 'date': '1816年7月9日', 'title': '阿根廷独立', 'e_id': '8551'},\n",
       "  {'day': '7/9', 'date': '1816年7月9日', 'title': '阿根廷独立节', 'e_id': '8552'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1843年7月9日',\n",
       "   'title': '杰出神经组织学家和病理学家戈尔季诞辰',\n",
       "   'e_id': '8553'},\n",
       "  {'day': '7/9', 'date': '1847年7月9日', 'title': '中国武术家黄飞鸿出生', 'e_id': '8554'},\n",
       "  {'day': '7/9', 'date': '1856年7月9日', 'title': '著名化学家阿伏加德罗逝世', 'e_id': '8555'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1858年7月9日',\n",
       "   'title': '美国人类学家法兰兹·鲍亚士诞生',\n",
       "   'e_id': '8556'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1888年7月9日',\n",
       "   'title': '现代出版家、商务印书馆总经理王云五诞生',\n",
       "   'e_id': '8557'},\n",
       "  {'day': '7/9', 'date': '1900年7月9日', 'title': '澳大利亚各殖民地组成联邦', 'e_id': '8558'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1900年7月9日',\n",
       "   'title': '清朝直隶提督聂士成在天津保卫战中英勇阵亡',\n",
       "   'e_id': '8559'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1910年7月9日',\n",
       "   'title': '有关耶路撒冷陷落的古代记录被发现',\n",
       "   'e_id': '8560'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1911年7月9日',\n",
       "   'title': '美国著名的物理学家约翰·阿奇博尔德·惠勒出生',\n",
       "   'e_id': '8561'},\n",
       "  {'day': '7/9', 'date': '1912年7月9日', 'title': '西方各国大肆发展海军', 'e_id': '8562'},\n",
       "  {'day': '7/9', 'date': '1919年7月9日', 'title': '曹锟、吴佩孚向段祺瑞宣战', 'e_id': '8563'},\n",
       "  {'day': '7/9', 'date': '1922年7月9日', 'title': '中共成立旅欧支部', 'e_id': '8564'},\n",
       "  {'day': '7/9', 'date': '1924年7月9日', 'title': '画坛泰斗黄永玉出生', 'e_id': '8565'},\n",
       "  {'day': '7/9', 'date': '1926年7月9日', 'title': '国民革命军誓师北伐', 'e_id': '8566'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1929年7月9日',\n",
       "   'title': '中国第一支少数民族红军创建',\n",
       "   'e_id': '8567'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1929年7月9日',\n",
       "   'title': '蒋阎张在北平西山密谈反冯玉祥',\n",
       "   'e_id': '8568'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1932年7月9日',\n",
       "   'title': '《洛桑会议》签订，停止德国赔款',\n",
       "   'e_id': '8569'},\n",
       "  {'day': '7/9', 'date': '1935年7月9日', 'title': '杜重远被判刑', 'e_id': '8570'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1938年7月9日',\n",
       "   'title': '三民主义青年团(三青团)成立',\n",
       "   'e_id': '8571'},\n",
       "  {'day': '7/9', 'date': '1943年7月9日', 'title': '盟军发起西西里岛登陆战役', 'e_id': '8572'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1949年7月9日',\n",
       "   'title': '中共希望与美国建立外交关系',\n",
       "   'e_id': '8573'},\n",
       "  {'day': '7/9', 'date': '1949年7月9日', 'title': '鞍山钢铁公司正式开工', 'e_id': '8574'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1956年7月9日',\n",
       "   'title': '著名电影演员汤姆·汉克斯诞辰',\n",
       "   'e_id': '8575'},\n",
       "  {'day': '7/9', 'date': '1962年7月9日', 'title': '邓子恢谈“包产到户”', 'e_id': '8576'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1964年7月9日',\n",
       "   'title': '意大利足球名将简卢卡·维亚利出生',\n",
       "   'e_id': '8577'},\n",
       "  {'day': '7/9', 'date': '1970年7月9日', 'title': '相声表演艺术家张寿臣逝世', 'e_id': '8578'},\n",
       "  {'day': '7/9', 'date': '1971年7月9日', 'title': '基辛格秘密访华', 'e_id': '8579'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1980年7月9日',\n",
       "   'title': '首届国际文化节在加纳首都阿克拉举行',\n",
       "   'e_id': '8580'},\n",
       "  {'day': '7/9', 'date': '1985年7月9日', 'title': '我国与玻利维亚建交', 'e_id': '8581'},\n",
       "  {'day': '7/9', 'date': '1986年7月9日', 'title': '中央鼓励科技人员合理流动', 'e_id': '8582'},\n",
       "  {'day': '7/9', 'date': '1986年7月9日', 'title': '中国首次赴北极考察', 'e_id': '8583'},\n",
       "  {'day': '7/9', 'date': '1988年7月9日', 'title': '我国与卡塔尔建交', 'e_id': '8584'},\n",
       "  {'day': '7/9',\n",
       "   'date': '1998年7月9日',\n",
       "   'title': '中共中央大型企业工委正式成立',\n",
       "   'e_id': '8585'},\n",
       "  {'day': '7/9', 'date': '1999年7月9日', 'title': '李登辉抛出“两国论”', 'e_id': '8586'},\n",
       "  {'day': '7/9', 'date': '2002年7月9日', 'title': '非盟首届首脑会议开幕', 'e_id': '8587'},\n",
       "  {'day': '7/9', 'date': '2005年7月9日', 'title': '台湾小说家郭松棻逝世', 'e_id': '8588'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2006年7月9日',\n",
       "   'title': '郑洁、晏紫首次捧起温网女双冠军奖杯',\n",
       "   'e_id': '8589'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2006年7月9日',\n",
       "   'title': '意大利获第18界世界杯足球赛冠军',\n",
       "   'e_id': '8590'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2007年7月9日',\n",
       "   'title': '全军首位“南丁格尔奖”获得者黎秀芳同志在兰州逝世',\n",
       "   'e_id': '8591'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2008年7月9日',\n",
       "   'title': '胡锦涛主席会见日本首相福田康夫',\n",
       "   'e_id': '8592'},\n",
       "  {'day': '7/9', 'date': '2009年7月9日', 'title': '美韩政府网站纷遭攻击', 'e_id': '8593'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2009年7月9日',\n",
       "   'title': '菲尔普斯打破男子100米蝶泳世界纪录',\n",
       "   'e_id': '8594'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2010年7月9日',\n",
       "   'title': '世界首艘“太阳帆船”成功实现机体变速',\n",
       "   'e_id': '8595'},\n",
       "  {'day': '7/9', 'date': '2015年7月9日', 'title': '双台风逼近我国东南沿海', 'e_id': '8596'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2016年7月9日',\n",
       "   'title': '空军原政委田修思因涉嫌严重违纪被立案审查',\n",
       "   'e_id': '8597'},\n",
       "  {'day': '7/9', 'date': '2017年7月9日', 'title': '伊拉克政府军收复摩苏尔', 'e_id': '8598'},\n",
       "  {'day': '7/9', 'date': '2018年7月9日', 'title': '小米正式在香港上市', 'e_id': '8599'},\n",
       "  {'day': '7/9',\n",
       "   'date': '2019年7月9日',\n",
       "   'title': '全球第一个实现5G全覆盖的国家诞生',\n",
       "   'e_id': '8600'},\n",
       "  {'day': '7/9', 'date': '2019年7月9日', 'title': '美国富翁罗斯·佩罗去世', 'e_id': '8601'}],\n",
       " 'error_code': 0}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "url_histody='http://v.juhe.cn/todayOnhistory/queryEvent.php'\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "my_key=\"5a5150f3e122472a8c8fdbb5e774e562\"\n",
    "date='7/9'\n",
    "request_url=url_histody+'?key='+my_key+'&date='+date\n",
    "request_url\n",
    "response = requests.get(request_url, headers=headers)\n",
    "res=response.json()\n",
    "res\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 包装成函数方便调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def histody(date):\n",
    "    import requests \n",
    "    url_histody='http://v.juhe.cn/todayOnhistory/queryEvent.php'\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    my_key=\"5a5150f3e122472a8c8fdbb5e774e562\"\n",
    "    request_url=url_histody+'?key='+my_key+'&date='+date\n",
    "    request_url\n",
    "    response = requests.get(request_url, headers=headers)\n",
    "    res=response.json()\n",
    "    return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'reason': 'success',\n",
       " 'result': [{'day': '7/5',\n",
       "   'date': '-717年7月5日',\n",
       "   'title': '古罗马建造者罗慕洛逝世',\n",
       "   'e_id': '8391'},\n",
       "  {'day': '7/5', 'date': '450年7月5日', 'title': '北魏太武帝族诛崔浩逝世', 'e_id': '8392'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1687年7月5日',\n",
       "   'title': '艾萨克·牛顿发表《自然哲学的数学原理》',\n",
       "   'e_id': '8393'},\n",
       "  {'day': '7/5', 'date': '1811年7月5日', 'title': '委内瑞拉独立', 'e_id': '8394'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1826年7月5日',\n",
       "   'title': '英国政治家斯坦福·莱佛士逝世',\n",
       "   'e_id': '8395'},\n",
       "  {'day': '7/5', 'date': '1830年7月5日', 'title': '法国占领阿尔及利亚', 'e_id': '8396'},\n",
       "  {'day': '7/5', 'date': '1833年7月5日', 'title': '法国发明家尼埃普斯逝世', 'e_id': '8397'},\n",
       "  {'day': '7/5', 'date': '1840年7月5日', 'title': '第一次鸦片战争爆发', 'e_id': '8398'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1854年7月5日',\n",
       "   'title': '《上海英法美租地章程》公布',\n",
       "   'e_id': '8399'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1857年7月5日',\n",
       "   'title': '国际妇女运动领袖克莱拉·蔡特金出生',\n",
       "   'e_id': '8400'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1893年7月5日',\n",
       "   'title': '《辞海》出版的功臣舒新城出生',\n",
       "   'e_id': '8401'},\n",
       "  {'day': '7/5', 'date': '1898年7月5日', 'title': '光绪帝下谕奖励新学人材', 'e_id': '8402'},\n",
       "  {'day': '7/5', 'date': '1911年7月5日', 'title': '法国总统蓬皮杜诞辰', 'e_id': '8403'},\n",
       "  {'day': '7/5', 'date': '1918年7月5日', 'title': '中华民国军政府组成', 'e_id': '8404'},\n",
       "  {'day': '7/5', 'date': '1928年7月5日', 'title': '孙殿英盗掘东陵', 'e_id': '8405'},\n",
       "  {'day': '7/5', 'date': '1930年7月5日', 'title': '周恩来在联共会议上作报告', 'e_id': '8406'},\n",
       "  {'day': '7/5', 'date': '1943年7月5日', 'title': '库尔斯克战役爆发', 'e_id': '8407'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1943年7月5日',\n",
       "   'title': '王稼祥首次提出“毛泽东思想”',\n",
       "   'e_id': '8408'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1946年7月5日',\n",
       "   'title': '世界上第一件比基尼泳装面世',\n",
       "   'e_id': '8409'},\n",
       "  {'day': '7/5', 'date': '1948年7月5日', 'title': '北平发生“七·五”惨案', 'e_id': '8410'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1957年7月5日',\n",
       "   'title': '马寅初发表“新人口论”遭厄运',\n",
       "   'e_id': '8411'},\n",
       "  {'day': '7/5', 'date': '1963年7月5日', 'title': '中共代表团抵达莫斯科', 'e_id': '8412'},\n",
       "  {'day': '7/5', 'date': '1976年7月5日', 'title': '中国首次远洋科学调查', 'e_id': '8413'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1977年7月5日',\n",
       "   'title': '巴基斯坦发生军事政变，总理布托被捕内幕',\n",
       "   'e_id': '8414'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1986年7月5日',\n",
       "   'title': '第一届友好运动会在莫斯科举行',\n",
       "   'e_id': '8415'},\n",
       "  {'day': '7/5', 'date': '1988年7月5日', 'title': '国家行政学院建立', 'e_id': '8416'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1988年7月5日',\n",
       "   'title': '张艺谋一人捧走百花奖和金鸡奖最佳男主角',\n",
       "   'e_id': '8417'},\n",
       "  {'day': '7/5', 'date': '1993年7月5日', 'title': '全国金融工作会议在京召开', 'e_id': '8418'},\n",
       "  {'day': '7/5',\n",
       "   'date': '1995年7月5日',\n",
       "   'title': '日本内阁总理大臣福田赳夫逝世',\n",
       "   'e_id': '8419'},\n",
       "  {'day': '7/5', 'date': '1996年7月5日', 'title': '克隆羊多莉的诞生', 'e_id': '8420'},\n",
       "  {'day': '7/5', 'date': '1998年7月5日', 'title': '克隆牛诞生', 'e_id': '8421'},\n",
       "  {'day': '7/5', 'date': '1998年7月5日', 'title': '香港启德机场正式关闭', 'e_id': '8422'},\n",
       "  {'day': '7/5', 'date': '1999年7月5日', 'title': '苏哈托起诉《时代》周刊', 'e_id': '8423'},\n",
       "  {'day': '7/5',\n",
       "   'date': '2003年7月5日',\n",
       "   'title': '世界卫生组织宣布已经成功控制SARS',\n",
       "   'e_id': '8424'},\n",
       "  {'day': '7/5', 'date': '2003年7月5日', 'title': '张爱萍上将逝世', 'e_id': '8425'},\n",
       "  {'day': '7/5',\n",
       "   'date': '2006年7月5日',\n",
       "   'title': '最伟大的皮划艇运动员盖德·弗雷德里克松逝世',\n",
       "   'e_id': '8426'},\n",
       "  {'day': '7/5', 'date': '2009年7月5日', 'title': '乌鲁木齐市发生骚乱', 'e_id': '8427'}],\n",
       " 'error_code': 0}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "histody('7/5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 天气调用接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': {'area': '汕头',\n",
       "  'city': '汕头',\n",
       "  'province': '广东',\n",
       "  'areaId': '101280501',\n",
       "  'now': {'sd': '64%',\n",
       "   'aqi': '19',\n",
       "   'temperature': '32',\n",
       "   'weather': '晴',\n",
       "   'wind_direction': '南风',\n",
       "   'weather_pic': 'https://img.jumdata.com/weather-icon/d00.png',\n",
       "   'wind_power': '2级'},\n",
       "  'dayWeathers': [{'night_low_temperature': '28',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220711'},\n",
       "   {'night_low_temperature': '27',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220712'},\n",
       "   {'night_low_temperature': '27',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220713'},\n",
       "   {'night_low_temperature': '27',\n",
       "    'night_wind_direction': '南风',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '33',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '南风',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220714'},\n",
       "   {'night_low_temperature': '27',\n",
       "    'night_wind_direction': '南风',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '03',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '33',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '阵雨',\n",
       "    'day_wind_direction': '南风',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d03.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220715'},\n",
       "   {'night_low_temperature': '26',\n",
       "    'night_wind_direction': '西南风',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n03.png',\n",
       "    'night_weather_code': '03',\n",
       "    'day_weather_code': '03',\n",
       "    'night_weather': '阵雨',\n",
       "    'day_high_temperature': '32',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '阵雨',\n",
       "    'day_wind_direction': '西南风',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d03.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220716'},\n",
       "   {'night_low_temperature': '28',\n",
       "    'night_wind_direction': '西南风',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n02.png',\n",
       "    'night_weather_code': '02',\n",
       "    'day_weather_code': '07',\n",
       "    'night_weather': '阴',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '3-4级',\n",
       "    'day_weather': '小雨',\n",
       "    'day_wind_direction': '西南风',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d07.png',\n",
       "    'night_wind_power': '3-4级',\n",
       "    'daytime': '20220717'}]},\n",
       " 'msg': '成功',\n",
       " 'success': True,\n",
       " 'code': 200,\n",
       " 'taskNo': '36794341511178862617'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "url = 'https://weatherquery.api.bdymkt.com/weather/query/by-area'\n",
    "params = {}\n",
    "area='汕头'\n",
    "params['area'] = area\n",
    "headers = {    \n",
    "    'Content-Type': 'application/json;charset=UTF-8',\n",
    "    'X-Bce-Signature': 'AppCode/7aff6227456548629e2f503add3d8825 '\n",
    "}\n",
    "r = requests.request(\"POST\", url, params=params, headers=headers)\n",
    "res=r.json()\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 包装成函数方便调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def weather_cx(area):\n",
    "    import requests\n",
    "    url = 'https://weatherquery.api.bdymkt.com/weather/query/by-area'\n",
    "    params = {}\n",
    "    params['area'] = area\n",
    "    headers = {    \n",
    "        'Content-Type': 'application/json;charset=UTF-8',\n",
    "    'X-Bce-Signature': 'AppCode/7aff6227456548629e2f503add3d8825 '\n",
    "    }\n",
    "    r = requests.request(\"POST\", url, params=params, headers=headers)\n",
    "    res=r.json()\n",
    "    return res\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': {'area': '潮州',\n",
       "  'city': '潮州',\n",
       "  'province': '广东',\n",
       "  'areaId': '101281501',\n",
       "  'now': {'sd': '59%',\n",
       "   'aqi': '22',\n",
       "   'temperature': '33',\n",
       "   'weather': '晴',\n",
       "   'wind_direction': '西南风',\n",
       "   'weather_pic': 'https://img.jumdata.com/weather-icon/d00.png',\n",
       "   'wind_power': '1级'},\n",
       "  'dayWeathers': [{'night_low_temperature': '26',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '35',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220711'},\n",
       "   {'night_low_temperature': '26',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '35',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220712'},\n",
       "   {'night_low_temperature': '25',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220713'},\n",
       "   {'night_low_temperature': '26',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n03.png',\n",
       "    'night_weather_code': '03',\n",
       "    'day_weather_code': '01',\n",
       "    'night_weather': '阵雨',\n",
       "    'day_high_temperature': '34',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '多云',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d01.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220714'},\n",
       "   {'night_low_temperature': '26',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n03.png',\n",
       "    'night_weather_code': '03',\n",
       "    'day_weather_code': '08',\n",
       "    'night_weather': '阵雨',\n",
       "    'day_high_temperature': '33',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '中雨',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d08.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220715'},\n",
       "   {'night_low_temperature': '26',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n01.png',\n",
       "    'night_weather_code': '01',\n",
       "    'day_weather_code': '03',\n",
       "    'night_weather': '多云',\n",
       "    'day_high_temperature': '33',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '阵雨',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d03.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220716'},\n",
       "   {'night_low_temperature': '25',\n",
       "    'night_wind_direction': '无持续风向',\n",
       "    'night_weather_pic': 'https://img.jumdata.com/weather-icon/n02.png',\n",
       "    'night_weather_code': '02',\n",
       "    'day_weather_code': '07',\n",
       "    'night_weather': '阴',\n",
       "    'day_high_temperature': '35',\n",
       "    'day_wind_power': '<3级',\n",
       "    'day_weather': '小雨',\n",
       "    'day_wind_direction': '无持续风向',\n",
       "    'day_weather_pic': 'https://img.jumdata.com/weather-icon/d07.png',\n",
       "    'night_wind_power': '<3级',\n",
       "    'daytime': '20220717'}]},\n",
       " 'msg': '成功',\n",
       " 'success': True,\n",
       " 'code': 200,\n",
       " 'taskNo': '74761000141303694375'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weather_cx('潮州')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 随机土味情话API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': 1, 'content': '听闻先生治家有方，小女余生愿闻其详'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "request_url='https://api.uomg.com/api/rand.qinghua?format=json'\n",
    "request_url\n",
    "response = requests.get(request_url, headers=headers)\n",
    "res=response.json()\n",
    "res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#  https://api.uomg.com/api/rand.qinghua?format=json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "303.819px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
